<?php 
error_reporting(E_ALL);
Zend_Loader::loadClass("BaseControllerAction");
Zend_Loader::loadClass("DXF_Parser");

class TestController extends BaseControllerAction
{
    
    public function init()
    {
    	$this->_helper->viewRenderer->setNoRender(true);
    	$this->_helper->layout->disableLayout();
    }

    public function parsedAction() 
    {
        $parse = new DXF_Parser();
        // Trovo le adiacenze delle varie superfici
        $app1 = array();//array d'appoggio
        $app2 = array();//array d'appoggio
        $adj = array();//matrice delle adiacenze
        $c1 = 0; //contatore che indica il numero di coordinate uguali
        $c2; //indice della matrice adiacenze
        
        //Ciclo esterno per creare la matrice delle adiacenze
        foreach ($parse as $k => $value)
        {
        	//assegno all'array d'appoggio il valore corrente
        	$app1 = $value;
        	while ($parse->hasNext()) 
        	{
        		$parse->next();
        		//Assegno al secondo array d'appoggio il valore successivo
        		$app2 =$parse->current(); 
        		$c2 = $parse->key();
        		for($i = 2; $i <= 5; $i++)
        		{
        			//Confronto i punti tra loro per vedere se sono uguali 
        			if(in_array($app1[$i], $app2))
        			{
        				$c1++;
        			}
        		}
        		//Se tutti e 4 i punti sono uguali carico i nomi delle sezioni 
        		//nella matrice delle adiacenze
        		if ($c1 == 4)
        		{
        			$adj[$k][$c2] = $app2[1];
        			$adj[$c2][$k] = $app1[1];
        			
        		}	
        		$c1 = 0;	
        	}
        	//Aggiorno l'indice dell'array per proseguire la ricerca
        	$parse->rewindTo($k);
        }
        print_r($adj);
        exit;
    }
    
    public function materialsAction()
	{
		$material_sql  = "TRUNCATE TABLE material;\n";
		$material_sql .= "TRUNCATE TABLE material_prop_physical;\n";
		$material_sql .= "TRUNCATE TABLE material_prop_thermal;\n";
		$material_sql .= "TRUNCATE TABLE material_prop_ecarbon;\n\n";

		/* 
		 * DesignBuilder database
		 *
		 * 0  => id
		 * 1  => Name
		 * 2  => CategoryId
		 * 3  => Conductivity (W/mK)
		 * 4  => Capacity (J/kgK)
		 * 5  => Density (kg/m3)
		 * 6  => Colour
		 * 7  => Texture
		 * 8  => Roughness
		 * 9  => ThermalAbsorptance
		 * 10 => SolarAbsorptance
		 * 11 => VisibleAbsorptance
		 * 12 => Simple
		 * 13 => Detailed
		 * 14 => R-Value
		 * 15 => Source
		 * 16 => RegionId
		 * 17 => Locked
		 * 18 => Scratch
		 * 19 => IsSystem
		 * 20 => Description
		 * 21 => DefaultThickness
		 * 22 => IsGreenRoof
		 * 23 => NameSoilLayer
		 * 24 => HeightOfPlants
		 * 25 => LeafAreaIndex
		 * 26 => LeafReflectivity
		 * 27 => LeafEmissivity
		 * 28 => MinStomatalRes
		 * 29 => MaxVolMoisture
		 * 30 => MinVolMoisture
		 * 31 => InitVolMoisture
		 * 32 => TransportId
		 * 33 => IsCO2Available
		 * 34 => IsCO2EAvailable
		 * 35 => EmbodiedCO2
		 * 36 => EmbodiedCO2E
		 * 37 => NameConvFactor
		 * 38 => DataSource
		 * 39 => Assumption
		 * 40 => EOL
		 *
    	 */
		$positions = array(
			 0 =>    0,		 1 =>    6,		 2 =>   98,
			 3 =>  110,		 4 =>  131,		 5 =>  149,
			 6 =>  166,		 7 =>  176,		 8 =>  185,
			 9 =>  200,		10 =>  220,		11 =>  238,
			12 =>  258,		13 =>  266,		14 =>  276,
			15 =>  285,		16 =>  419,		17 =>  429,
			18 =>  437,		19 =>  458,		20 =>  468,
			21 =>  708,		22 =>  726,		23 =>  739,
			24 =>  754,		25 =>  770,		26 =>  785,
			27 =>  803,		28 =>  819,		29 =>  835,
			30 =>  851,		31 =>  867,		32 =>  884,
			33 =>  897,		34 =>  913,		35 =>  930,
			36 =>  943,		37 =>  957,		38 => 1028,
			39 => 1052,		40 => 1189	
		);
    	
		$fp = fopen(APPLICATION_PATH . DS . 'resources' . DS . 'materials.dat', "r");
		fgets($fp);
		fgets($fp);
    	while(($row = fgets($fp)) !== false) {
			$elem = array();
    		for ($i = 0; $i < count($positions) - 1; $i++)
    		{
    			$elem[$i] = substr($row, $positions[$i], ($positions[$i+1] - $positions[$i]));
    			$elem[$i] = trim($elem[$i], " #\n\r\t");
			}

			$material_sql .= "-- " . strtoupper($elem[1]);
			$material_tpl = <<<EOQ
INSERT INTO material SET 
	id              = %d,
	name            = '%s',
	description     = '%s',
	id_category     = %d,
	thickness       = %f,
	rvalue          = %f,
	has_details     = %d,
	has_rvalue      = %d,
	has_greenroof   = %d,
	has_ecarbon     = %d
;
EOQ;
			$material_sql .= "\n" . sprintf(
				$material_tpl,
				$elem[0],
				mysql_escape_string($elem[1]),
				mysql_escape_string($elem[20]),
				$elem[2],
				$elem[21],
				!empty($elem[14]) ? $elem[14] : null,
				$elem[13],
				$elem[12],
				$elem[22],
				$elem[33]
			);


			$material_prop_physical_tpl = <<<EOQ
INSERT INTO material_prop_physical SET
	id                  = %d,
	thermal_absorptance = %f,
	solar_absorptance   = %f,
	visible_absorptance = %f,
	roughness           = %d
;
EOQ;
			$material_sql .= "\n" . sprintf(
				$material_prop_physical_tpl,
				$elem[0],
				$elem[9],
				$elem[10],
				$elem[11],
				(int)$elem[8]
			);
			
			if ((int)$elem[13] == 1) {
				$material_prop_thermal_tpl = <<<EOQ
INSERT INTO material_prop_thermal SET
	id              = %d,
	conductivity    = %f,
	capacity        = %f,
	density         = %f
;
EOQ;
				$material_sql .= "\n" . sprintf(
					$material_prop_thermal_tpl,
					$elem[0],
					$elem[3],
					$elem[4],
					$elem[5]
				);
			}

			if ((int)$elem[33] == 1) {
				$material_prop_ecarbon_tpl = <<<EOQ
INSERT INTO material_prop_ecarbon SET
	id                  = %d,
	co2                 = %f,
	conv_factor_name    = '%s',
	source              = '%s',
	assumption          = '%s',
	has_co2e            = %d,
	co2e                = %f
;
EOQ;
				$material_sql .= "\n" . sprintf(
					$material_prop_ecarbon_tpl,
					$elem[0],
					$elem[35],
					mysql_escape_string($elem[37]),
					mysql_escape_string($elem[38]),
					mysql_escape_string($elem[39]),
					$elem[34],
					!empty($elem[36]) ? $elem[36] : null
				);


			}
			
			$material_sql .= "\n\n";
    		
		}
		echo $material_sql;
		fclose ($fp);	
    }
    
    public function constructionsAction()
	{
		$construction_sql  = "TRUNCATE TABLE construction;\n";
		$construction_sql .= "TRUNCATE TABLE construction_prop_physical;\n";
		$construction_sql .= "TRUNCATE TABLE construction_prop_thermal;\n";

		/* 
		 * DesignBuilder database
		 *
		 * 0 => #Id
		 * 1 => #Name
		 * 2 => #CategoryId
		 * 3 => #U-Value
		 * 4 => #SRO
		 * 5 => #SRI
		 * 6 => #HCO
		 * 7 => #HCI
		 * 8 => #RadO
		 * 9 => #RadI
		 * 10 => #SolarAbsorptivity
		 * 11 => #Mat 1
		 * 12 => #Mat 2
		 * 13 => #Mat 3
		 * 14 => #Mat 4
		 * 15 => #Mat 5
		 * 16 => #Mat 6
		 * 17 => #Mat 7
		 * 18 => #Mat 8
		 * 19 => #Mat 9
		 * 20 => #Mat 10
		 * 21 => #Mat 11
		 * 22 => #Mat 12
		 * 23 => #Mat 13
		 * 24 => #Mat 14
		 * 25 => #Mat 15
		 * 26 => #Mat 16
		 * 27 => #Mat 17
		 * 28 => #Mat 18
		 * 29 => #Mat 19
		 * 30 => #Mat 20
		 * 31 => #Thick 1
		 * 32 => #Thick 2
		 * 33 => #Thick 3
		 * 34 => #Thick 4
		 * 35 => #Thick 5
		 * 36 => #Thick 6
		 * 37 => #Thick 7
		 * 38 => #Thick 8
		 * 39 => #Thick 9
		 * 40 => #Thick 10
		 * 41 => #Thick 11
		 * 42 => #Thick 12
		 * 43 => #Thick 13
		 * 44 => #Thick 14
		 * 45 => #Thick 15
		 * 46 => #Thick 16
		 * 47 => #Thick 17
		 * 48 => #Thick 18
		 * 49 => #Thick 19
		 * 50 => #Thick 20
		 * 51 => #NumberLayers
		 * 52 => #ParamSelected
		 * 53 => #BMat 1
		 * 54 => #BMat 2
		 * 55 => #BMat 3
		 * 56 => #BMat 4
		 * 57 => #BMat 5
		 * 58 => #BMat 6
		 * 59 => #BMat 7
		 * 60 => #BMat 8
		 * 61 => #BMat 9
		 * 62 => #BMat 10
		 * 63 => #BMat 11
		 * 64 => #BMat 12
		 * 65 => #BMat 13
		 * 66 => #BMat 14
		 * 67 => #BMat 15
		 * 68 => #BMat 16
		 * 69 => #BMat 17
		 * 70 => #BMat 18
		 * 71 => #BMat 19
		 * 72 => #BMat 20
		 * 73 => #PercBridge 1
		 * 74 => #PercBridge 2
		 * 75 => #PercBridge 3
		 * 76 => #PercBridge 4
		 * 77 => #PercBridge 5
		 * 78 => #PercBridge 6
		 * 79 => #PercBridge 7
		 * 80 => #PercBridge 8
		 * 81 => #PercBridge 9
		 * 82 => #PercBridge 10
		 * 83 => #PercBridge 11
		 * 84 => #PercBridge 12
		 * 85 => #PercBridge 13
		 * 86 => #PercBridge 14
		 * 87 => #PercBridge 15
		 * 88 => #PercBridge 16
		 * 89 => #PercBridge 17
		 * 90 => #PercBridge 18
		 * 91 => #PercBridge 19
		 * 92 => #PercBridge 20
		 * 93 => #Bridged 1
		 * 94 => #Bridged 2
		 * 95 => #Bridged 3
		 * 96 => #Bridged 4
		 * 97 => #Bridged 5
		 * 98 => #Bridged 6
		 * 99 => #Bridged 7
		 * 100 => #Bridged 8
		 * 101 => #Bridged 9
		 * 102 => #Bridged 10
		 * 103 => #Bridged 11
		 * 104 => #Bridged 12
		 * 105 => #Bridged 13
		 * 106 => #Bridged 14
		 * 107 => #Bridged 15
		 * 108 => #Bridged 16
		 * 109 => #Bridged 17
		 * 110 => #Bridged 18
		 * 111 => #Bridged 19
		 * 112 => #Bridged 20
		 * 113 => #URL
		 * 114 => #LRL
		 * 115 => #BSUValue
		 * 116 => #IsSystem
		 * 117 => #Source
		 * 118 => #RegionId
		 * 119 => #Locked
		 * 120 => #Scratch
		 * 121 => #UValueSurfaceToSurface
		 * 122 => #RValue
		 * 123 => #BSRValue
		 * 124 => #BSUValueSurfaceToSurface
		 * 125 => #HideInLists
		 * 126 => #DefinitionMethod
		 * 127 => #Thickness
		 * 128 => #InsideTexture
		 * 129 => #OutsideTexture
		 * 130 => #CM
		 * 131 => #InvolvesMetalCladding
		 * 132 => #SBEMType
		 * 133 => #Description
		 * 134 => #CM2
		 * 135 => #LibraryCategory
		 * 136 => #LibraryConstructionName
		 * 137 => #InferenceSector
		 * 138 => #InferenceRegs
		 * 139 => #InferenceGD
		 * 140 => #GroundFloorUValueCorrected
		 * 141 => #OverrideDefaultHCO
		 * 142 => #ManualHCO
		 * 143 => #OverrideDefaultHCI
		 * 144 => #ManualHCI
		 * 145 => #CostPerArea
		 * 146 => #IsInternalSource
		 * 147 => #InternalSourceLayer
		 * 148 => #InternalSourceDimensions
		 * 149 => #InternalSourceTubeSpacing
		 *
    	 */
		$positions = array(
				0 => 0,
				1 => 6,
				2 => 143,
				3 => 155,
				4 => 177,
				5 => 182,
				6 => 187,
				7 => 205,
				8 => 223,
				9 => 241,
				10 => 259,
				11 => 278,
				12 => 285,
				13 => 292,
				14 => 299,
				15 => 306,
				16 => 313,
				17 => 320,
				18 => 327,
				19 => 334,
				20 => 341,
				21 => 349,
				22 => 357,
				23 => 365,
				24 => 373,
				25 => 381,
				26 => 389,
				27 => 397,
				28 => 405,
				29 => 413,
				30 => 421,
				31 => 429,
				32 => 451,
				33 => 473,
				34 => 495,
				35 => 517,
				36 => 539,
				37 => 561,
				38 => 577,
				39 => 593,
				40 => 609,
				41 => 625,
				42 => 641,
				43 => 657,
				44 => 673,
				45 => 689,
				46 => 705,
				47 => 721,
				48 => 737,
				49 => 753,
				50 => 769,
				51 => 785,
				52 => 799,
				53 => 814,
				54 => 822,
				55 => 830,
				56 => 838,
				57 => 846,
				58 => 854,
				59 => 862,
				60 => 870,
				61 => 878,
				62 => 886,
				63 => 895,
				64 => 904,
				65 => 913,
				66 => 922,
				67 => 931,
				68 => 940,
				69 => 949,
				70 => 958,
				71 => 967,
				72 => 976,
				73 => 985,
				74 => 999,
				75 => 1013,
				76 => 1027,
				77 => 1041,
				78 => 1055,
				79 => 1069,
				80 => 1083,
				81 => 1097,
				82 => 1111,
				83 => 1126,
				84 => 1141,
				85 => 1156,
				86 => 1171,
				87 => 1186,
				88 => 1201,
				89 => 1216,
				90 => 1231,
				91 => 1246,
				92 => 1261,
				93 => 1276,
				94 => 1287,
				95 => 1298,
				96 => 1309,
				97 => 1320,
				98 => 1331,
				99 => 1342,
				100 => 1353,
				101 => 1364,
				102 => 1375,
				103 => 1387,
				104 => 1399,
				105 => 1411,
				106 => 1423,
				107 => 1435,
				108 => 1447,
				109 => 1459,
				110 => 1471,
				111 => 1483,
				112 => 1495,
				113 => 1507,
				114 => 1525,
				115 => 1543,
				116 => 1565,
				117 => 1575,
				118 => 1691,
				119 => 1701,
				120 => 1709,
				121 => 1730,
				122 => 1754,
				123 => 1772,
				124 => 1790,
				125 => 1816,
				126 => 1829,
				127 => 1848,
				128 => 1870,
				129 => 1885,
				130 => 1901,
				131 => 1923,
				132 => 1946,
				133 => 1974,
				134 => 2226,
				135 => 2248,
				136 => 2265,
				137 => 2290,
				138 => 2307,
				139 => 2322,
				140 => 2335,
				141 => 2363,
				142 => 2383,
				143 => 2394,
				144 => 2414,
				145 => 2425,
				146 => 2438,
				147 => 2456,
				148 => 2477,
				149 => 2503,
				150 => 2529,
				
		);
    	
		$fp = fopen(APPLICATION_PATH . DS . 'resources' . DS . 'constructions.dat', "r");
		fgets($fp);
		fgets($fp);
    	while(($row = fgets($fp)) !== false) {
			$elem = array();
    		for ($i = 0; $i < count($positions) - 1; $i++)
    		{
    			$elem[$i] = substr($row, $positions[$i], ($positions[$i+1] - $positions[$i]));
    			$elem[$i] = trim($elem[$i], " #\n\r\t");
			}

			$construction_sql .= "-- " . strtoupper($elem[1]);
			$construction_tpl = <<<EOQ
INSERT INTO construction SET 
	id              = %d,
	id_category     = %d,
	name            = '%s',
	description     = '%s',
	source			= '%s'
;
EOQ;
			$construction_sql .= "\n" . sprintf(
				$construction_tpl,
				$elem[0],
				$elem[2],
				mysql_escape_string($elem[1]),
				mysql_escape_string($elem[133]),
				$elem[117]
			);


			$construction_prop_physical_tpl = <<<EOQ
INSERT INTO construction_prop_physical SET
	id          = %d,
	rad_i       = %f,
	rad_o       = %f,
	sri			= %f,
	hco			= %f,
	hci			= %f,
	sro			= %f
;
EOQ;
			$construction_sql .= "\n" . sprintf(
				$construction_prop_physical_tpl,
				$elem[0],
				$elem[9],
				$elem[8],
				$elem[5],
				$elem[6],
				$elem[7],
				$elem[4]
			);
			
			$construction_prop_thermal_tpl = <<<EOQ
INSERT INTO construction_prop_thermal SET
	id              = %d,
	uvalue_sts		= %f,
	rvalue			= %f,
	uvalue			= %f,
	url				= %f,
	lrl				= %f,
	bs_uvalue_sts   = %f,
	bs_rvalue		= %f,
	bs_uvalue		= %f
;
EOQ;
				$construction_sql .= "\n" . sprintf(
					$construction_prop_thermal_tpl,
					$elem[0],
					$elem[121],
					$elem[122],
					$elem[3],
					$elem[113],
					$elem[114],
					$elem[124],
					$elem[123],
					$elem[115]
				);
			
			$construction_sql .= "\n\n";
    		
		}
		echo $construction_sql;
		fclose ($fp);	
    }
    
    public function relConstrMatAction() {
    	// Mat: 11 - 30
    	// Thk: 31 - 50
    	$baseMat = 11;
    	$baseThk = 31;
    	$positions = array(
    			0 => 0,
    			1 => 6,
    			2 => 143,
    			3 => 155,
    			4 => 177,
    			5 => 182,
    			6 => 187,
    			7 => 205,
    			8 => 223,
    			9 => 241,
    			10 => 259,
    			11 => 278,
    			12 => 285,
    			13 => 292,
    			14 => 299,
    			15 => 306,
    			16 => 313,
    			17 => 320,
    			18 => 327,
    			19 => 334,
    			20 => 341,
    			21 => 349,
    			22 => 357,
    			23 => 365,
    			24 => 373,
    			25 => 381,
    			26 => 389,
    			27 => 397,
    			28 => 405,
    			29 => 413,
    			30 => 421,
    			31 => 429,
    			32 => 451,
    			33 => 473,
    			34 => 495,
    			35 => 517,
    			36 => 539,
    			37 => 561,
    			38 => 577,
    			39 => 593,
    			40 => 609,
    			41 => 625,
    			42 => 641,
    			43 => 657,
    			44 => 673,
    			45 => 689,
    			46 => 705,
    			47 => 721,
    			48 => 737,
    			49 => 753,
    			50 => 769,
				51 => 785,
				52 => 799,
    	);
    	
    	$rel_sql  = "TRUNCATE TABLE rel_construction_material;\n";

    	$fp = fopen(APPLICATION_PATH . DS . 'resources' . DS . 'constructions.dat', "r");
    	fgets($fp);
    	fgets($fp);
    	while(($row = fgets($fp)) !== false) {
    		$counter =  0;
    		$elem = array();
    		for ($i = 0; $i < count($positions) - 1; $i++)
    		{
    			$elem[$i] = substr($row, $positions[$i], ($positions[$i+1] - $positions[$i]));
    			$elem[$i] = trim($elem[$i], " #\n\r\t");
    		}
    	
    		$rel_sql .= "-- " . strtoupper($elem[1]);
    		$rel_tpl = <<<EOQ
INSERT INTO rel_construction_material SET
	id_construction = %d,
	id_material     = %d,
	layer           = %d,
	thickness       = %.9f
;
EOQ;
    		do {
    			if (!empty($elem[($baseMat + $counter)])) {
	    			$rel_sql .= "\n" . sprintf(
	    					$rel_tpl,
	    					$elem[0],
	    					$elem[($baseMat + $counter)],
	    					($counter + 1),
	    					$elem[($baseThk + $counter)]
	    			);
	    			
    			}
    		} while (++$counter < 20);

    		$rel_sql .= "\n\n";
    	}
    	echo $rel_sql;
    	fclose ($fp);
    }
    
    public function headerAction() {
    	$fp = fopen(APPLICATION_PATH . DS . 'resources' . DS . 'constructions_header.dat', "r");
    	$line = fgets($fp);
    	fclose($fp);
    	
    	$pos_array = array();
    	$pos = $lastpos = 0;
    	while (true) {
    		$pos = strpos($line, '#', $lastpos);
    		$pos_array[] = $pos;
    		$lastpos = $pos + 1;
    		if ($pos === false) break;
    	}
    	foreach ($pos_array as $k => $v) {
    		echo "$k => $v,\n";
    	}
    	
    	foreach ($pos_array as $k => $v) {
    		echo "$k => " . trim(substr($line, $v, ($pos_array[$k+1] - $pos_array[$k]))) . "\n";
    	}
    	
    	//echo "<pre>" . print_r($pos_array,1);
    	
    }
    
    public function databaseAction() {
    	$dbAdapter = Zend_Db_Table::getDefaultAdapter();
    	$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
    	
    	$sql = 'SELECT * FROM construction';
    	$result = $dbAdapter->fetchAll($sql);
    	
    	print_r($result);
    	
    }
}

?>
